// https://leetcode.cn/problems/subdomain-visit-count/
// Created by ade on 2022/10/9.
//
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    vector <string> subdomainVisits(vector <string> &cpdomains) {
        unordered_map<string, int> m = {};
        for (auto s : cpdomains) {
            int pos = s.find_first_of(' ');
            int num = stoi(s.substr(0, pos));
            string tmp = s.substr(pos + 1);
            m[tmp] += num;
            int p = 0;
            while (p = tmp.find('.', p)) {
                if(p == -1) break;
                m[tmp.substr(p + 1)] += num;
                p++;
                if(p >= tmp.size()) break;
            }
        }
        vector <string> res = {};
        for (auto v : m) {
            res.push_back(to_string(v.second) + " " + v.first);
        }
        return res;
    }
};

int main() {
    Solution so;
    vector <string> cpdomains = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
    auto res = so.subdomainVisits(cpdomains);
    for (auto s : res) {
        cout << s << ",";
    }
    cout << endl;
    return 0;
}